from app.db import BaseDocument
import mongoengine as db
import datetime


class SitePortRaits(BaseDocument):
    """
     "_id": ObjectId("608bae0433d5a6001d9770f8"),
     "is_changed": False,
     "version": 32,
     "uid": ObjectId("604d7940405c990011467f39"),
     "target": "https://www.cccccc.com:8080",
     "portrait": "{}",
     "created_at": ISODate("2021-04-30T07:13:08.390Z")
     "updated_at": ISODate("2021-04-30T07:13:08.390Z")

    Args:
        db ([type]): [description]

    Returns:
        [type]: [description]
    """
    uid = db.ObjectIdField()
    target = db.StringField()
    version = db.IntField(default=-1)
    is_changed = db.BooleanField(default=False)
    portrait = db.StringField(default='{}')
    created_at = db.DateTimeField()
    updated_at = db.DateTimeField()

    meta = {
        "collection": "site_portraits",
        "strict": False,
        "indexes": [
            {"fields": ["uid", "target"]},
        ]
    }

    def save(self, *args, **kwargs):
        if not self.created_at:
            self.created_at = datetime.datetime.utcnow()
        if not self.updated_at:
            self.updated_at = datetime.datetime.utcnow()
        return super(SitePortRaits, self).save(*args, **kwargs)


class SitePortRaitTask(BaseDocument):
    uid = db.ObjectIdField()
    target = db.StringField()
    version = db.IntField(default=-1)
    is_changed = db.BooleanField(default=True)
    patches = db.StringField(default='{}')
    created_at = db.DateTimeField()
    updated_at = db.DateTimeField()

    meta = {
        'collection': 'site_portrait_tasks',
        'strict': False,
        "indexes": [
            {"fields": ["uid", "target", "-created_at"]}
        ]
    }

    def save(self, *args, **kwargs):
        if not self.created_at:
            self.created_at = datetime.datetime.utcnow()
        if not self.updated_at:
            self.updated_at = datetime.datetime.utcnow()
        return super(SitePortRaitTask, self).save(*args, **kwargs)
